﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

//using System.Runtime.Serialization;
//using System.Runtime.Serialization.Json;
using System.IO;

namespace formworks
{
    /// <summary>
    /// Json序列和反序列化操作
    /// </summary>
    public class JsonBag
    {

        /// <summary>
        /// 数据节点
        /// </summary>
        public struct data
        {
            public object key;
            public object value;
        }

        /// <summary>
        /// 逗号
        /// </summary>
        public static string _comma = ",";
        /// <summary>
        /// 花括号 开始
        /// </summary>
        public static string _sbrace = "{";
        /// <summary>
        /// 花括号 结束
        /// </summary>
        public static string _ebrace = "}";
        /// <summary>
        /// 方括号 开始
        /// </summary>
        public static string _ssquare = "[";
        /// <summary>
        /// 方括号 结束
        /// </summary>
        public static string _esquare = "]";

        
        /// <summary>
        /// 序列化一个对象到Json
        /// </summary>
        /// <typeparam name="T">要序列化的对象类型</typeparam>
        /// <param name="model">要序列化的实例化对象</param>
        /// <returns></returns>
        public static string serializeModel<T>(T model)
        {
            //DataContractJsonSerializer serializer = 
            //    new DataContractJsonSerializer(model.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                //serializer.WriteObject(ms, model);
                string retVal = Encoding.UTF8.GetString(ms.ToArray());
                return retVal;
            }            
        }


        /// <summary>
        /// 序列化许多对象到Json
        /// </summary>
        /// <typeparam name="T">要序列化的对象类型<</typeparam>
        /// <param name="modelList">要序列化的实例化对象列表</param>
        /// <returns></returns>
        public static string serializeModelList<T>(List<T> modelList)
        {
            //DataContractJsonSerializer serializer =
            //    new DataContractJsonSerializer(modelList.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                //serializer.WriteObject(ms, modelList);
                string retVal = Encoding.UTF8.GetString(ms.ToArray());
                return retVal;
            }
        }


        /// <summary>
        /// 序列化一个对象到json
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string serializeObject(object obj)
        {
            //DataContractJsonSerializer serializer =
            //    new DataContractJsonSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                //serializer.WriteObject(ms, obj);
                string retVal = Encoding.UTF8.GetString(ms.ToArray());
                return retVal;
            }
        }



        /// <summary>
        /// *****暂时有个未知 BUG ****
        /// 反序列化Json到一个对象
        /// </summary>
        /// <typeparam name="T">要序列化的对象类型</typeparam>
        /// <param name="json">json字符串</param>
        /// <returns></returns>
        public static T deserializeModel<T>(string json)
        {
            T model = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                //DataContractJsonSerializer serializer = new DataContractJsonSerializer(model.GetType());
                //model = (T)serializer.ReadObject(ms);
                ms.Close();
                return model;
            }
        }


        /// <summary>
        /// 反序列化Json到一个默认对象
        /// </summary>
        /// <param name="json">json字符串</param>
        /// <returns></returns>
        public static object deserializeObject(string json)
        {
            object model = new object();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                //DataContractJsonSerializer serializer = new DataContractJsonSerializer(model.GetType());
                //model = serializer.ReadObject(ms);
                ms.Close();
                return model;
            }
        }


        /// <summary>
        /// 反序列化Json到对象列表
        /// </summary>
        /// <typeparam name="T">要序列化的对象类型</typeparam>
        /// <param name="json">json字符串</param>
        /// <returns></returns>
        public static List<T> deserializeModelList<T>(string json)
        {
            List<T> modelList = Activator.CreateInstance<List<T>>();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                //DataContractJsonSerializer serializer =
                //    new DataContractJsonSerializer(modelList.GetType());
                //modelList = (List<T>)serializer.ReadObject(ms);
                ms.Close();
                return modelList;
            }
        }

        /// <summary>
        /// 格式化数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string format(object key, object value)
        {
            return string.Format("\"{0}\":{1}", 
                new object[]
                {
                    key, 
                    value
                });
        }

    }//end class
}//end namespace
